<template>
  <div class="app-container">
    <!-- 条件搜索 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属乡镇" prop="townShipId">
        <el-select v-model="form.townShipId" placeholder="请选择">
          <el-option v-for="item in townList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="场所名称" prop="venueName">
        <el-input v-model="queryParams.venueName" placeholder="请输入住宿地名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="管理员" prop="venueAdminId">
        <el-select v-model="queryParams.venueAdminId" clearable filterable placeholder="请选择">
          <el-option v-for="item in adminList" :key="item.id" :label="item.userName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 页面操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['view:accommodation_introduction:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['view:accommodation_introduction:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['view:accommodation_introduction:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['view:accommodation_introduction:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- table表格 -->
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="名称" align="center" prop="venueName" />
      <el-table-column label="所属乡镇" align="center" prop="townShipName" />
      <el-table-column label="图片" align="center" prop="image">
        <template slot-scope="scope">
          <el-image v-for="(item, index) in scope.row.priveImg" :key="index" :src="item"
            :preview-src-list="scope.row.priveImg" :lazy="true" alt style="width:50px;height:50px;"></el-image>
        </template>
      </el-table-column>
      <!-- <el-table-column label="介绍" align="center" prop="venueIntroduce">
        <template slot-scope="scope">
          <div v-html="scope.row.venueIntroduce" style="height:100px;overflow:hidden">
          </div>
        </template>
      </el-table-column> -->
      <el-table-column label="地址" align="center" prop="venueAddress" />
      <el-table-column label="联系电话" align="center" prop="venuePhone" />
      <el-table-column label="管理员" align="center" prop="venueAdminIdName" />
      <!-- <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['view:accommodation_introduction:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['view:accommodation_introduction:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改住宿详情页面对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1000px" append-to-body
      @close="dioClose">
      <!-- <div class="preview-win" v-html="form.content"></div> -->
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="场所名称" prop="venueName">
          <el-input v-model="form.venueName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="场所图片" prop="image">
          <imageUpload v-model="form.image" @uploadImage="uploadimg" @remove="removeImg" :limit="6" />
        </el-form-item>
        <el-form-item label="场所介绍" prop="venueIntroduce">
          <editor v-if="open" v-model="form.venueIntroduce" :min-height="192" />
        </el-form-item>
        <el-form-item label="联系电话" prop="venuePhone">
          <el-input v-model="form.venuePhone" type="number" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="所属乡镇" prop="townShipId">
          <el-select v-model="form.townShipId" placeholder="请选择">
            <el-option v-for="item in townList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="场所地址" prop="venueAddress">
          <zxMap :value="latLng" :address="form.venueAddress" @input="mapClick" v-if="open"></zxMap>
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请选择" disabled />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请选择" disabled />
        </el-form-item>
        <el-form-item label="管理员" prop="venueAdminId">
          <el-select v-model="form.venueAdminId" clearable multiple filterable placeholder="请选择">
            <el-option v-for="item in adminList" :key="item.id" :label="item.userName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { place_list, place_add, place_update, place_delete, adminList, place_info } from "@/api/view/accommodation_introduction";
import ImageUpload from "@/components/UploadImage";
import zxMap from "@/components/map/index";
import Editor from '@/components/Editor';
import { town_list, } from "@/api/view/activity_information";
export default {
  name: "Accommodation_introduction",
  components: {
    ImageUpload,
    Editor,
    zxMap
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场所表格事件
      list: [],
      // 乡镇列表
      townList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        townShipId: null,
        venueName: null,
        venueAdminId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        venueName: [{ required: true, message: "请输入", trigger: "blur" }],
        venuePhone: [{ required: true, message: "请输入", trigger: "blur" }],
        venueIntroduce: [{ required: true, message: "请输入", trigger: "blur" }],
        venueAddress: [{ required: true, message: "请输入", trigger: "blur" }],
        image: [{ required: true, message: "请选择", trigger: "change" }],
        townShipId: [{ required: true, message: "请输入", trigger: "blur" }],
        latitude: [{ required: true, message: "请在上方地图中选择", trigger: "blur" }],
        longitude: [{ required: true, message: "请在上方地图中选择", trigger: "blur" }],
        // venueAdminId: [{ required: true, message: "请选择管理员", trigger: "blur" }],
      },
      latLng: "",
      adminList: [], //管理员列表
    };
  },
  created() {
    if(this.$route.query.searchName){
       this.queryParams.venueName = this.$route.query.searchName;
       this.getList();
    };
    
    this.getTownList();
    this.getList();
    this.getAdminList();
  },
  methods: {
    /* 获取乡镇列表 */
    async getTownList() {
      let res = await town_list();
      let { code, data } = res;
      if (code === 200) {
        this.townList = data;
      };
    },
    /** 查询场所列表数据 */
    async getList() {
      let res = await place_list(this.queryParams);
      let { code, rows, total } = res;
      if (code === 200) {
        this.loading = true;
        rows.forEach((item) => {
          let priveImg = []; //图片预览数组
          item.imagesUrl = item.imageScale.split(",");
          item.imagesUrl.forEach((val) => {
            priveImg.push(this.baseurl + val);
            item.priveImg = priveImg;
          });
        });
        this.list = rows;
        this.total = total;
        this.loading = false;
      };
    },
    //获取管理员列表
    getAdminList() {
      adminList().then(res => {
        this.adminList = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        venueName: null,
        venuePhone: null,
        venueIntroduce: null,
        image: null,
        venueAddress: null,
        townShipId: null,
        latitude: null,
        longitude: null,
        venueAdminId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加场所";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "修改场所";
      const id = row.id || this.ids;
      place_info({ id: id }).then(res => {
        this.latLng = res.data.longitude + "," + res.data.latitude;
        res.data.venueAdminId = !res.data.venueAdminId ? [] : res.data.venueAdminId.split(",").map(Number);
        this.form = res.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(async valid => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.form));
          data.venueAdminId = data.venueAdminId.join(",");
          if (data.id != null) {
            let res = await place_update(data);
            if (res.code === 200) {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            };
          } else {
            let res = await place_add(data);
            if (res.code === 200) {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            };

          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除场所名称为"' + row.venueName + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        let res = await place_delete(ids);
        if (res.code === 200) {
          this.msgSuccess("删除成功");
          this.getList();
        };
      }).catch(() => {
        this.msgSuccess("取消删除");
      })
    },
    /** 导出按钮操作 */
    // handleExport() {
    //   const queryParams = this.queryParams;
    //   this.$confirm('是否确认导出所有住宿详情页面数据项?', "警告", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   }).then(function () {

    //   }).then(response => {
    //     this.download(response.msg);
    //   })
    // },
    //图片上传事件
    uploadimg(e) {
      this.form.image = e;
    },
    //删除图片事件
    removeImg(e) {
      console.log(e, "remove");
      this.form.image = e;
    },
    /* 地图事件 */
    mapClick(e) {
      let latlng = e.lnglat.split(",");
      this.form.longitude = latlng[0];
      this.form.latitude = latlng[1];
      this.form.venueAddress = e.address;
      console.log(e, "emap");
    },
    /* dio关闭时触发 */
    dioClose() {
      this.latLng = "";
    },
  }
};
</script>